#!/usr/bin/env bash

# Filename     :	centos8_system_init.sh
# Last modified:	2022-09-27 12:12
# Version      :
# Author       : jack.zang
# Email        : jack.zang@aishangwei.net
# Description  :
# 使用方法：source <(curl -sL https://gitee.com/jack_zang/public-scripts/raw/master/shell/system/centos8_system_init.sh)
# ******************************************************

## 交互输入主机名，IP 地址
read -t 30 -p "Please Input HostName：" HOSTNAME
echo "系统网卡名称： $(cat /proc/net/dev | awk '{i++;if(i>2)print $1}' |sed 's/[:]*$//g')"
read -t 30 -p "Please Input NetWork Device：" NETDEV
read -t 30 -p "Please Input HostIP：" HOSTIP
read -t 30 -p "Please Input HostGateway：" HOSTGATEWAY
read -t 30 -p "Please Input DNS：" HOSTDNS

## 设置主机名
if [ -n "$HOSTNAME" ]; then
  hostnamectl set-hostname $HOSTNAME
else
  echo "HostName 为空，不设置主机名"
fi


## 设置IP
if [ -n "$NETDEV" ] && [ -n "$HOSTIP" ] && [ -n "$HOSTGATEWAY" ] && [ -n "$HOSTDNS" ]; then
  sed -i '/^BOOTPROTO*/c\BOOTPROTO=none' /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^IPADDR" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" && sed -i "/^IPADDR*/c\IPADDR=${HOSTIP}" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^IPADDR" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" || echo "IPADDR=${HOSTIP}" >> /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^PREFIX" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" && sed -i "/^PREFIX*/c\PREFIX=24" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^PREFIX" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" || echo "PREFIX=24" >> /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^GATEWAY" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" && sed -i "/^GATEWAY*/c\GATEWAY=${HOSTGATEWAY}" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^GATEWAY" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" || echo "GATEWAY=${HOSTGATEWAY}" >> /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^DNS" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" && sed -i "/^DNS*/c\DNS1=${HOSTDNS}" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  grep "^DNS" /etc/sysconfig/network-scripts/ifcfg-"$NETDEV" || echo "DNS1=${HOSTDNS}" >> /etc/sysconfig/network-scripts/ifcfg-"$NETDEV"
  systemctl restart NetworkManager
  nmcli networking off && nmcli networking on
else
  echo "网卡名称、主机IP、网关、DNS 为空，不设置"
fi


## 关闭防火墙和 Selinux
systemctl disable --now firewalld
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

## 配置为阿里云 yum 仓库
source <(curl -sL https://gitee.com/jack_zang/public-scripts/raw/master/shell/repo/centos8_use_aliyun_base_epel.sh)

## 时间同步
dnf install -y chrony
sed -i '/^pool 2.centos/d' /etc/chrony.conf
cat >> /etc/chrony.conf <<EOF
server ntp.aliyun.com iburst
server cn.ntp.org.cn iburst
EOF
systemctl restart chronyd && systemctl enable chronyd
chronyc  sources -v

## 时区配置为中国-上海
timedatectl set-timezone Asia/Shanghai

## 配置系统句柄数
ulimit -SHn 65535
cat >> /etc/security/limits.conf <<EOF
* soft nofile 655360
* hard nofile 131072
* soft nproc 655350
* hard nproc 655350
* seft memlock unlimited
* hard memlock unlimitedd
EOF


